﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RM.Common
{
    /// <summary>
    /// 模块的权限列表
    /// 在此为每个模块定义可用的权限列表
    /// </summary>
    public enum ModulePowerType
    {
        None,
        /// <summary>
        /// 浏览权限是公共的，每个模块都有
        /// </summary>
        CanRead,
        //核心菜单模块定义了三种权限
        CoreMenu_New,
        CoreMenu_Edit,
        CoreMenu_Delete,

        CoreDept_New,
        CoreDept_Edit,
        CoreDept_Delete,

        CoreRole_New,
        CoreRole_Edit,
        CoreRole_Delete,

        CoreUser_New,
        CoreUser_Edit,
        CoreUser_Delete,
        CoreUser_ChangePassword,

        CoreRoleUser_New,
        CoreRoleUser_Delete,

        CoreRoleModule_Edit,

        //CoreRoleMenu_Edit,

        CoreLog_Delete,

        //CoreConf_Edit,

        CorePassword_Edit
    }

    /// <summary>
    /// 模块权限帮助类
    /// </summary>
    public partial class ModulePowerTypeHelper
    {
        public static string GetPowerTypeName(string modulePowerTypeName)
        {
            return GetPowerTypeName(modulePowerTypeName, String.Empty);
        }
        public static string GetPowerTypeName(ModulePowerType powerType)
        {
            return GetPowerTypeName(ModulePowerTypeHelper.Power2String(powerType), String.Empty);
        }
        public static string GetPowerTypeName(ModulePowerType powerType, string moduleTypeName)
        {
            return GetPowerTypeName(ModulePowerTypeHelper.Power2String(powerType), moduleTypeName);
        }
        /// <summary>
        /// 由模块名称和模块权限名称获取权限的名称
        /// CoreUser_ChangePassword => ChangePassword
        /// CoreUser_ChangePassword, CoreRole => String.Empty
        /// </summary>
        /// <param name="modulePowerTypeName">模块权限名称（CoreUser_ChangePassword）</param>
        /// <param name="moduleTypeName">模块名称（CoreUser）</param>
        /// <returns>权限的名称（ChangePassword）</returns>
        public static string GetPowerTypeName(string modulePowerTypeName, string moduleTypeName)
        {
            string powerTypeName = String.Empty;
            int underscoreIndex = modulePowerTypeName.IndexOf('_');
            if (underscoreIndex >= 0)
            {
                string moduleName = modulePowerTypeName.Substring(0, underscoreIndex);
                powerTypeName = modulePowerTypeName.Substring(underscoreIndex + 1);

                // 如果指定的模块前缀不相同
                if (!String.IsNullOrEmpty(moduleTypeName) && moduleName != moduleTypeName)
                {
                    powerTypeName = String.Empty;
                }

            }
            return powerTypeName;
        }

        /// <summary>
        /// 字符串转换为ModulePowerType
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static ModulePowerType String2Power(string text)
        {
            return String2Power(text, false);
        }

        /// <summary>
        /// 字符串转换为ModulePowerType
        /// </summary>
        /// <param name="text"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public static ModulePowerType String2Power(string text, bool ignoreCase)
        {
            if (Enum.IsDefined(typeof(ModulePowerType), text))
            {
                return (ModulePowerType)Enum.Parse(typeof(ModulePowerType), text, ignoreCase);
            }
            else
            {
                return ModulePowerType.None;
            }
        }

        /// <summary>
        /// ModulePowerType转换为字符串
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static String Power2String(ModulePowerType type)
        {
            return type.ToString();
        }

        /// <summary>
        /// 获取所有权限的名称列表
        /// </summary>
        /// <returns></returns>
        public static string[] GetModulePowerTypes()
        {
            return Enum.GetNames(typeof(ModulePowerType));
        }

        private static Dictionary<string, List<string>> _moduleOtherPowerTypeDic = new Dictionary<string, List<string>>();
        /// <summary>
        /// 获得每个模块对应的其他权限名称列表（Others字段）返回值为["New","Edit","Delete"]
        /// </summary>
        /// <param name="moduleTypeName">模块名称（比如CoreDept）</param>
        /// <returns></returns>
        public static List<string> GetModuleOtherPowerTypeDic(string moduleTypeName)
        {
            if (!_moduleOtherPowerTypeDic.ContainsKey(moduleTypeName))
            {
                _moduleOtherPowerTypeDic[moduleTypeName] = new List<string>();
                foreach (string otherPower in GetModulePowerTypes())
                {
                    int underscoreIndex = otherPower.IndexOf('_');
                    if (underscoreIndex >= 0)
                    {
                        string moduleName = otherPower.Substring(0, underscoreIndex);
                        if (moduleTypeName == moduleName)
                        {
                            string powerName = otherPower.Substring(underscoreIndex + 1);
                            if (!_moduleOtherPowerTypeDic[moduleTypeName].Contains(powerName))
                            {
                                _moduleOtherPowerTypeDic[moduleTypeName].Add(powerName);
                            }
                        }
                    }
                }
            }
            return _moduleOtherPowerTypeDic[moduleTypeName];
        }

    }
}
